package com.hubt.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hubt.common.PageBean;
import com.hubt.mapper.PtMenuDirectoryMapper;
import com.hubt.mapper.PtRoleMapper;
import com.hubt.mapper.PtRoleResMapper;
import com.hubt.model.PtMenuDirectory;
import com.hubt.model.PtRole;
import com.hubt.model.PtRoleRes;
import com.hubt.service.PtRoleService;
import com.hubt.vo.Linkvo;
import com.hubt.vo.RoleVo;

@Service("/ptroleService")
public class PtRoleServiceImpl implements PtRoleService {

	@Autowired
	private PtRoleMapper ptrolemapper;
	
	@Autowired
	private PtMenuDirectoryMapper ptmenuDirectoryMapper;
	
	@Autowired
	private PtRoleResMapper prs;
	@Override
	public PageBean<RoleVo> findbyPage(int pageNumber, int pageSize) {
		// TODO Auto-generated method stub
		List<PtRole> list=ptrolemapper.findAllByPage((pageNumber-1)*pageSize, pageSize);
		List<RoleVo> list2=new ArrayList<RoleVo>();
		for(PtRole t:list) {
			RoleVo roleVo=new RoleVo();
			roleVo.setRole_id(t.getRoleId());
			roleVo.setRole_name(t.getRoleName());
			roleVo.setRole_uuid(t.getRoleUuid());
			list2.add(roleVo);
		}
		PageBean<RoleVo> pageBean=new PageBean<RoleVo>();
		pageBean.setRows(list2);
		int total=ptrolemapper.countSize();
		pageBean.setTotal(total);
		pageBean.setPageNumber(pageNumber);
		pageBean.setPageSize(pageSize);
		pageBean.setTotalPage((total%pageNumber==0)?(total/pageNumber):(total/pageSize+1));
		return pageBean;
	}

	@Override
	public int insert(RoleVo roleVo) {
		// TODO Auto-generated method stub
		try {
			PtRole ptRole=new PtRole();
			ptRole.setRoleId(roleVo.getRole_id());
			ptRole.setRoleName(roleVo.getRole_name());
			ptRole.setRoleUuid(roleVo.getRole_id());
			ptrolemapper.insert(ptRole);
			return 1;
		} catch (Exception e) {
			// TODO: handle exception
			return 0;
		}
		
	}

	@Override
	public RoleVo findbyId(String id) {
		// TODO Auto-generated method stub
		PtRole ptRole=ptrolemapper.selectByPrimaryKey(id);
		RoleVo roleVo=new RoleVo();
		roleVo.setRole_id(ptRole.getRoleId());
		roleVo.setRole_name(ptRole.getRoleName());
		roleVo.setRole_uuid(ptRole.getRoleUuid());
		
		return roleVo;
	}

	@Override
	public int update(RoleVo roleVo) {
		// TODO Auto-generated method stub
//		try {
			PtRole ptRole=new PtRole();
			ptRole.setRoleId(roleVo.getRole_id());
			ptRole.setRoleName(roleVo.getRole_name());
			ptRole.setRoleUuid(roleVo.getRole_uuid());
			ptrolemapper.updateByPrimaryKeySelective(ptRole);
			return 1;
//		} catch (Exception e) {
//			// TODO: handle exception
//			return 0;
//		}
		
	}

	@Override
	public int remove(String id) {
		// TODO Auto-generated method stub
		try {
			if(prs.findById(id)==null) {
				ptrolemapper.deleteByPrimaryKey(id);
			}else {
				prs.delete(id);
				ptrolemapper.deleteByPrimaryKey(id);
			}
			return 1;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return 0;
	}

	@Override
	public List<String> getResources(String roleId) {
		// TODO Auto-generated method stub
		List<String> idList=new ArrayList<String>();
		List<PtMenuDirectory> directories=ptmenuDirectoryMapper.getResources(roleId);
		for(PtMenuDirectory ptMenuDirectory:directories) {
			idList.add(ptMenuDirectory.getMenuId());
		}
		return idList;
	}

	@Override
	public int linkRes(Linkvo linkvo) {
		// TODO Auto-generated method stub
		try {
			String roleId=linkvo.getRoleId();
			String[] resId=linkvo.getResources().split(",");
			String res=new String();
			if(!(prs.findById(roleId)==null)) {
				prs.delete(roleId);
				}
			for(int i=0;i<resId.length;i++) {
				System.out.println(resId[i]);
				PtRoleRes ptRoleRes=new PtRoleRes();
				ptRoleRes.setResUuid(resId[i]);
				ptRoleRes.setRoleUuid(roleId);
				prs.insert(ptRoleRes);
			}
			return 1;
		} catch (Exception e) {
			// TODO: handle exception
			return 0;
		}
		
		
	}

	@Override
	public List<RoleVo> findAll() {
		// TODO Auto-generated method stub
		List<RoleVo> list=new ArrayList<RoleVo>();
		List<PtRole> list2=ptrolemapper.findAll();
		for(PtRole t:list2) {
			RoleVo roleVo=new RoleVo();
			roleVo.setRole_id(t.getRoleId());
			roleVo.setRole_name(t.getRoleName());
			roleVo.setRole_uuid(t.getRoleUuid());
			list.add(roleVo);
			
		}
		return list;
	}

}
